package com.coder.algorithm.offer_2;

import java.util.Arrays;

/**
 * 数组中唯一只出现一次的数字
 *
 * @author yuhushuan
 * @date 2020/6/30 23:57
 */
public class _56_2_NumberAppearingOnce {
    private static int findNumberAppearingOnce(int[] numbers) {
        int[] bitSum = new int[32];

        for (int number : numbers) {
            for (int i = 0; i < 32; ++i) {
                // bitSum[0] 为低位
                if (((number >> i) & 1) == 1) {
                    bitSum[i]++;
                }
            }
        }


        int number = 0;
        for (int i = 0; i < 32; ++i) {
            if (bitSum[i] % 3 == 1) {
                number += (1 << i);
            }
        }
        return number;
    }

    public static void main(String[] args) {
        // 功能测试
        // 唯一出现的一次的数字分别是 0，正数、负数
        print(new int[]{10, 10, 10, 21, 35, 35, 35, 41, 41, 41});
        print(new int[]{1, 1, 1, 0, 3, 3, 3, 4, 4, 4});
        print(new int[]{1, 1, 1, 2, 3, 3, 3, 4, 4, 4});
        print(new int[]{1, 1, 1, -2, 3, 3, 3, 4, 4, 4});

        // 出现三次的的数字分别是 0，正数、负数
        print(new int[]{0, 0, 0, -2});
        print(new int[]{4, 4, 4, -2});
        print(new int[]{-4, -4, -4, -2});
        print(new int[]{4});

    }

    private static void print(int[] number) {
        System.out.println("数组：" + Arrays.toString(number));
        System.out.println("唯一出现一次的数字是：" + findNumberAppearingOnce(number));
        System.out.println();
    }

}
